package com.whzykj.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.whzykj.jfinal.TableBind;

@TableBind(name="price",pk="id")
public class Price extends Model<Price>{
	private static final long serialVersionUID = 1L;
	public static final Price me=new Price();
	
	/****
	 * 查询商品价格
	 * @param goods_id
	 * @return
	 */
	public Record getPriceById(String goods_id)
	{
		if(goods_id == null || "".equals(goods_id)) return null;
		String couponQry = " select * from price where goods_id = ? and date(set_date) = curdate() ";
		Record rd = Db.findFirst(couponQry,goods_id);		
		if(rd == null)
		{
			couponQry = " select * from price where goods_id = ? order by set_date desc ";
			rd = Db.findFirst(couponQry,goods_id);	
		}
		return rd;		
	}
	
	/****
	 * 查询商品价格
	 * @param goods_id
	 * @return
	 */
	public Record getPriceById(String goods_id,String curdate)
	{
		if(goods_id == null || "".equals(goods_id)) return null;
		String couponQry = " select * from price where goods_id = ? and set_date=? ";
		Record rd = Db.findFirst(couponQry,goods_id,curdate);
		return rd;		
	}
	
	/****
	 * 查询商品价格
	 * @param goods_id
	 * @return
	 */
	public Record getPriceById(String goods_id,String curdate,String station_id)
	{
		if(goods_id == null || "".equals(goods_id)) return null;
		String couponQry = " select * from price where goods_id = ? and set_date=? and station_id =? ";
		Record rd = Db.findFirst(couponQry,goods_id,curdate,station_id);
		return rd;		
	}
	
	/****
	 * 查询商品价格
	 * @param goods_id
	 * @return
	 */
	public Record getPriceBystation(String goods_id,String station_id)
	{
		if(goods_id == null || "".equals(goods_id)) return null;
		String couponQry = " select * from price where goods_id = ? and station_id =? ";
		Record rd = Db.findFirst(couponQry,goods_id,station_id);
		return rd;		
	}
	
	/**
	 * 寻找次日商品的全站通用价格是否确定
	 * @return
	 */
	public int findNextDayPrice(){
		String sql="SELECT COUNT(1) from price where TO_DAYS(set_date)=TO_DAYS(now())+1 and station_id=-1";
		int cnt=Db.queryLong(sql).intValue();
		return cnt;
	}
}
